package ArraySecond;
/**
 * 零矩阵
 * 编写一种算法，若M × N矩阵中某个元素为0，则将其所在的行与列清零。
 * <p>
 * 示例 1：
 * <p>
 * 输入：
 * [
 * [1,1,1],
 * [1,0,1],
 * [1,1,1]
 * ]
 * 输出：
 * [
 * [1,0,1],
 * [0,0,0],
 * [1,0,1]
 * ]
 *
 * @param
 */
public class Array8 {
    public static void rotate(int[][] matrix) {
        int row = matrix.length;
        int col = matrix[0].length;
        boolean[] isColZero = new boolean[col];
        boolean[] isRowZero = new boolean[row];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if(matrix[i][j]==0){
                    isColZero[j]=true;
                    isRowZero[i]=true;
                }
            }
        }

        for (int i = 0; i < row; i++) {
            if(isRowZero[i]){
                for (int j = 0; j < col; j++) {
                    matrix[i][j]=0;
                }
            }
        }

        for (int i = 0; i < col; i++) {
            if(isColZero[i]){
                for (int j = 0; j < row; j++) {
                    matrix[j][i]=0;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1,1,1},{1,0,1},{1,1,1}};
        rotate(matrix);
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                System.out.print(matrix[i][j]);
            }
            System.out.println();
        }
    }
}
